package com.logistics.web;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.logistics.AdminUser;
import com.logistics.admin.BaseAction;
import com.logistics.service.AdminUserService;

@Component("adminUserAdminAction")
@Scope("prototype")
public class AdminUserAdminAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4111553458414574334L;
	
	private AdminUserService service;
	
	@Autowired
	public void setService(AdminUserService service) {
		this.service = service;
	}

	private AdminUser user;
	
	public AdminUser getUser() {
		return user;
	}

	public void setUser(AdminUser user) {
		this.user = user;
	}

	private String uid;

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	private String newPassword;

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	private String oldPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	public void changePasswordFroJson() {
		if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword) ) {
			outputInvalidStatus(500);
			return;
		}
		
		AdminUser user = getLogonAdminUser();
		
		if (user == null) {
			outputInvalidStatus(500);
			return;
		}
		
		try {
			service.changePassword(user.getEntityId(), oldPassword, newPassword);
		} catch (Exception e) {
			outputInvalidStatus(500);
			return;
		}
		
		outputSimpleSuccessJson();
	}
}
